// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.border;

import java.util.HashMap;

import org.dengues.reports.editor.design.utils.DEUtil;
import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.swt.SWT;

/**
 * Base class for border
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public abstract class BaseBorder extends AbstractBorder {

    public static final String STYLE_NONO = "none";

    /**
     * Bottom border width.
     */
    public String bottomWidth;

    /**
     * Bottom border style.
     */
    public String bottomStyle;

    /**
     * Bottom border color.
     */
    public int bottomColor;

    /**
     * Top border width.
     */
    public String topWidth;

    /**
     * Top border style.
     */
    public String topStyle;

    /**
     * Top border color.
     */
    public int topColor;

    /**
     * Left border width.
     */
    public String leftWidth;

    /**
     * Left border style.
     */
    public String leftStyle;

    /**
     * Left border color.
     */
    public int leftColor;

    /**
     * Right border width.
     */
    public String rightWidth;

    /**
     * Right border style.
     */
    public String rightStyle;

    /**
     * Right border color.
     */
    public int rightColor;

    protected int i_bottom_style, i_bottom_width = 1;

    protected int i_top_style, i_top_width = 1;

    protected int i_left_style, i_left_width = 1;

    protected int i_right_style, i_right_width = 1;

    private static final HashMap styleMap = new HashMap();

    private static final HashMap widthMap = new HashMap();

    private static final double EPS = 1.0E-10;

    protected int leftGap, rightGap, bottomGap, topGap;

    static {
        styleMap.put("solid", new Integer(SWT.LINE_SOLID));//$NON-NLS-1$
        styleMap.put("dotted", new Integer(SWT.LINE_DOT));//$NON-NLS-1$
        styleMap.put("dashed", new Integer(SWT.LINE_DASH));//$NON-NLS-1$
        styleMap.put("double", new Integer(-2));//$NON-NLS-1$
        styleMap.put(STYLE_NONO, new Integer(0));//$NON-NLS-1$

        widthMap.put("thin", new Integer(1));//$NON-NLS-1$
        widthMap.put("medium", new Integer(2));//$NON-NLS-1$
        widthMap.put("thick", new Integer(3));//$NON-NLS-1$
    }

    /**
     * Returns the border width as pixel.
     * 
     * @param obj
     * @return
     */
    protected int getBorderWidth(Object obj) {
        // handle non-predefined values.
        if (obj instanceof String) {
            String[] rt = DEUtil.splitString((String) obj);

            if (rt[0] != null && DEUtil.isValidNumber(rt[0])) {
                double w = 0;

                // if the width is too small,
                // think it's zero
                if (w <= EPS) {
                    return 0;
                }

                // if the width is not too small;
                // think it's minimum size is 1
                return Math.max(1, (int) w);
            }
        }

        // handle predefined values.
        Integer retValue = (Integer) (widthMap.get(obj));

        if (retValue == null) {
            return 1;
        }

        return retValue.intValue();
    }

    /**
     * Since the insets now include border and padding, use this to get the true and non-revised border insets.
     * 
     * @return border insets.
     */
    public abstract Insets getTrueBorderInsets();

    /**
     * Since the insets now include border and padding, use this to get the border insets. This value may be revised
     * according to specified element.
     * 
     * @return border insets.
     */
    public abstract Insets getBorderInsets();

    /**
     * Sets the insets for padding.
     * 
     * @param in
     */
    public abstract void setPaddingInsets(Insets in);

    /**
     * Returns the border style.
     * 
     * @param obj
     * @return
     */
    protected int getBorderStyle(Object obj) {
        Integer retValue = (Integer) (styleMap.get(obj));
        if (retValue == null) {
            // fix bug 168627.the default style is silid.
            return SWT.LINE_SOLID;
            // return SWT.LINE_DASH;
        }

        return retValue.intValue();
    }

    /**
     * Convenient method to return the specified border style directly.
     * 
     * @return
     */
    public int getLeftBorderStyle() {
        return getBorderStyle(leftStyle);
    }

    /**
     * Convenient method to return the specified border width directly.
     * 
     * @return
     */
    public int getLeftBorderWidth() {
        return getBorderWidth(leftWidth);
    }

    /**
     * Convenient method to return the specified border style directly.
     * 
     * @return
     */
    public int getRightBorderStyle() {
        return getBorderStyle(rightStyle);
    }

    /**
     * Convenient method to return the specified border width directly.
     * 
     * @return
     */
    public int getRightBorderWidth() {
        return getBorderWidth(rightWidth);
    }

    /**
     * Convenient method to return the specified border width directly.
     * 
     * @return
     */
    public int getTopBorderWidth() {
        return getBorderWidth(topWidth);
    }

    /**
     * Convenient method to return the specified border width directly.
     * 
     * @return
     */
    public int getBottomBorderWidth() {
        return getBorderWidth(bottomWidth);
    }

    /**
     * Convenient method to return the specified border style directly.
     * 
     * @return
     */
    public int getTopBorderStyle() {
        return getBorderStyle(topStyle);
    }

    /**
     * Convenient method to return the specified border style directly.
     * 
     * @return
     */
    public int getBottomBorderStyle() {
        return getBorderStyle(bottomStyle);
    }

    /**
     * Convenient method to return the specified border color directly.
     * 
     * @return
     */
    public int getLeftBorderColor() {
        // return ColorUtil.parseColor( leftColor );
        return leftColor;
    }

    /**
     * Convenient method to return the specified border color directly.
     * 
     * @return
     */
    public int getRightBorderColor() {
        // return ColorUtil.parseColor( rightColor );
        return rightColor;
    }

    /**
     * Convenient method to return the specified border color directly.
     * 
     * @return
     */
    public int getTopBorderColor() {
        // return ColorUtil.parseColor( topColor );
        return topColor;
    }

    /**
     * Convenient method to return the specified border color directly.
     * 
     * @return
     */
    public int getBottomBorderColor() {
        // return ColorUtil.parseColor( bottomColor );
        return bottomColor;
    }

}